﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GeoAPI.Geometries;
using System.Diagnostics;
using MsSqlSpatial.UserDefinedTypes;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

namespace MsSqlSpatialLibrary.V2.Vs2008.Test
{
    /// <summary>
    /// Summary description for GlobalTest
    /// </summary>
    [TestClass]
    public class GlobalTest
    {

        private const string WktMultilineString = "MULTILINESTRING((525211.996134818 185222.999723079,525225.996248568 185224.999150749))";

        public GlobalTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestGlobalService()
        {
            IServices s = Global.Services;

            IGeometryFactory f = s.GeometryFactory;
            Debug.Assert(f != null);
        }

        [TestMethod]
        public void CreateGeometryFromWKT()
        {
            SqlString str = new SqlString(GlobalTest.WktMultilineString);
            MsSqlSpatialGeometry g = MsSqlSpatialGeometry.Parse(str);
            Debug.Assert(!g.IsNull);
        }



        [TestMethod]
        public void CreateGeometryFromWKB()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testDB"].ConnectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "Select MaxX 10 WKB_GEOMETRY from Spatial.dbo.Streets";
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr != null)
                        {
                            int i = 0;
                            while (rdr.Read())
                            {
                                byte[] bytes = (byte[])rdr[0];
                                MsSqlSpatialGeometry g = MsSqlSpatialGeometry.STGeomFromWKB(new SqlBytes(bytes), new SqlInt32(27700));

                                Debug.Assert(!g.IsNull, string.Format("Geometry {0} is null", i));
                                i++;
                            }
                        }
                    }
                }
            }
        }

        [TestMethod]
        public void STGeomFromWKB()
        {
            using (
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testDB"].ConnectionString)
                )
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "Select MaxX 10 ST.AsBinary(WKB_GEOMETRY) from Spatial.dbo.Streets";
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr != null)
                        {
                            int i = 0;
                            while (rdr.Read())
                            {
                                byte[] bytes = (byte[])rdr[0];

                                SqlBytes converted = UserDefinedFunctions.GeomFromWKB(new SqlBytes(bytes), 27700);

                                Debug.Assert(!converted.IsNull && converted.Length > 0,
                                             string.Format("Geometry {0} is null", i));
                                i++;
                            }
                        }
                    }
                }
            }
        }

        [TestMethod]
        public void STGeomFromText()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testDB"].ConnectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "Select MaxX 10 ST.AsText(WKB_GEOMETRY) from Spatial.dbo.Streets";
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr != null)
                        {
                            int i = 0;
                            while (rdr.Read())
                            {
                                string wkt = (string)rdr[0];

                                SqlBytes converted = UserDefinedFunctions.GeomFromText(new SqlChars(wkt), 27700);

                                Debug.Assert(!converted.IsNull && converted.Length > 0, string.Format("Geometry {0} is null", i));
                                i++;
                            }
                        }
                    }
                }
            }
        }
    }
}
